#include "catch.hpp"

#include "common/re.h"


TEST_CASE("re tests", "[re]") {
    using namespace std;
    using namespace evm::re;

    SECTION("poker pair") {

        SECTION("valid hand") {
            auto re = compile(R"re(^[a2-9tjqk]{5}$)re");

            string hand = "akt5q";
            auto m = re.match(hand);
            REQUIRE(m);
            REQUIRE(m.group() == hand);

            REQUIRE(!re.match("akt5e"));
            REQUIRE(!re.match("akt"));

            hand = "727ak";
            m = re.match(hand);
            REQUIRE(m);
            REQUIRE(m.group() == hand);
        }

        SECTION("hand contains a pair, back reference") {
            auto re = compile(R"re(.*(.).*\1)re");

            auto m = re.match("717ak");
            REQUIRE(m);
            REQUIRE(m.count() == 2);
            REQUIRE(m.group() == "717");
            REQUIRE(m.group(1) == "7");
            REQUIRE_THROWS(m.group(2));

            m = re.match("718ak");
            REQUIRE(!m);
            REQUIRE_THROWS(m.group());

            m = re.match("354aa");
            REQUIRE(m);
            REQUIRE(m.group() == "354aa");
        }
    }

    SECTION("search vs match") {
        REQUIRE(!match("c", "abcde"));
        REQUIRE(search("c", "abcde", NONE));
        REQUIRE(!search("^c", "abcde"));
        REQUIRE(search("^a", "abcde"));
    }

    SECTION("simple North American telephone number with an optional area code") {
        auto re = compile(R"re(^(\([0-9]{3}\))?[0-9]{3}-[0-9]{4}$)re");
        REQUIRE(re.match("555-1212"));
        REQUIRE(re.search("555-1212"));
    }
}
